import multiprocessing as mu
import time
from data_divd import util
def get_log(message):
    print(time.strftime("%Y-%m-%d %H:%M:%S")+f": {message}")

def get_file(file_name,number):
    f = open(file_name, "rb")
    max_number = f.seek(0, 2)
    print(max_number)
    ava_number = max_number // number
    remain_number = max_number%number
    if remain_number:
        need_read=(ava_number if i<number else remain_number for i in range(number+1))
    else:
        need_read = (ava_number for _ in range(number))
    f.seek(0, 0)
    res=[]
    for item in need_read:
        res.append(f.read(item))
    res.insert(0,file_name)
    res=tuple(res)
    return res

def process_write(top,type_name):
    with open(util.dive_file()+"."+type_name, "wb") as f:
        f.write(top)
        get_log(top)


def main(*args):
    type_name = args[0][0].split(".")[-1]
    processes=[]
    for arg in args[0][1:]:
        if type(arg)==str:
            type_name=arg.split(".")[1]
        else:
            p=mu.Process(target=process_write,args=(arg,type_name))
            processes.append(p)
            p.start()
            p.join()


if __name__=="__main__":
    args=get_file("test.png", 3)
    main(args)

